/**
 * ExportCsvFileTemplate.java
 * Created at 2016年4月8日
 * Created by Jackie Liu
 * Copyright (C) 2016, All rights reserved.
 */
package com.huabo.excel;

import com.huabo.utils.FileUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.PrintWriter;
import java.util.List;

/**
 * <p>ClassName: ExportCsvFileTemplate</p>
 * <p>Description: 导出csv文件</p>
 * <p>Author: Jackie Liu</p>
 * <p>Date: 2016年4月8日</p>
 */
public class ExportCsvFileTemplate<T> implements FileExport<T> {

	private StringBuilder builder;
	private List<T> list;
	private String[] titles;
	
	/**
	 * <p>Description: 无参构造函数</p>
	 */
	public ExportCsvFileTemplate() {
		builder = new StringBuilder();
	}
	
	/**
	 * <p>Description: 构造对象时注入数据</p>
	 * @param list 导出数据
	 */
	public ExportCsvFileTemplate(List<T> list) {
		builder = new StringBuilder();
		this.list = list;
	}
	
	/* (non-Javadoc)
	 * <p>Title: buildFile</p>
	 * <p>Description: </p>
	 * @param callBack
	 * @see com.huabo.spsp.export.FileExport#buildFile(com.huabo.spsp.export.BuildFileCallBack)
	 */
	@Override
	public void buildFile(BuildFileCallBack<T> callBack) {
		if (list == null) {
			throw new NullPointerException("数据不存在");
		}
		if (callBack == null) {
			throw new NullPointerException("解析方式回调对象不存在");
		}
		
		for (T t : list) {
			callBack.execute(t);
			this.newLine();
		}
	}

	/* (non-Javadoc)
	 * <p>Title: appendData</p>
	 * <p>Description: </p>
	 * @param data
	 * @see com.huabo.spsp.export.FileExport#appendData(java.lang.String)
	 */
	@Override
	public void appendData(String data) {
		builder.append("\t").append(data).append(",");
	}

	/* (non-Javadoc)
	 * <p>Title: appendData</p>
	 * <p>Description: </p>
	 * @param data
	 * @see com.huabo.spsp.export.FileExport#appendData(int)
	 */
	@Override
	public void appendData(int data) {
		builder.append("\t").append(data).append(",");
	}

	/* (non-Javadoc)
	 * <p>Title: appendData</p>
	 * <p>Description: </p>
	 * @param data
	 * @see com.huabo.spsp.export.FileExport#appendData(float)
	 */
	@Override
	public void appendData(float data) {
		builder.append("\t").append(data).append(",");
	}

	/* (non-Javadoc)
	 * <p>Title: appendData</p>
	 * <p>Description: </p>
	 * @param data
	 * @see com.huabo.spsp.export.FileExport#appendData(double)
	 */
	@Override
	public void appendData(double data) {
		builder.append("\t").append(data).append(",");
	}

	/* (non-Javadoc)
	 * <p>Title: appendData</p>
	 * <p>Description: </p>
	 * @param data
	 * @see com.huabo.spsp.export.FileExport#appendData(boolean)
	 */
	@Override
	public void appendData(boolean data) {
		builder.append(data).append(",");
	}

	/* (non-Javadoc)
	 * <p>Title: export</p>
	 * <p>Description: </p>
	 * @param targetPath
	 * @see com.huabo.spsp.export.FileExport#export(java.lang.String)
	 */
	@Override
	public void export(String targetPath) {
		FileUtils.write(new File(targetPath), (String) export());
	}

	/* (non-Javadoc)
	 * <p>Title: export</p>
	 * <p>Description: </p>
	 * @return
	 * @see com.huabo.spsp.export.FileExport#export()
	 */
	@Override
	public Object export() {
		if (titles != null && titles.length > 0) {
			StringBuilder sb = new StringBuilder();
			for (String title : titles) {
				sb.append(title).append(",");
			}
			sb.append("\r\n").append(builder);
			return sb.toString();
		}
		return builder.toString();
	}
	
	/* (non-Javadoc)
	 * <p>Title: newLine</p>
	 * <p>Description: </p>
	 * @see com.huabo.spsp.export.FileExport#newLine()
	 */
	@Override
	public void newLine() {
		builder.append("\r\n");
	}

	/* (non-Javadoc)
	 * <p>Title: setTitle</p>
	 * <p>Description: </p>
	 * @param titles
	 * @see com.huabo.spsp.export.FileExport#setTitle(java.lang.String[])
	 */
	@Override
	public void setTitle(String[] titles) {
		this.titles = titles;
	}

	/* (non-Javadoc)
	 * <p>Title: clear</p>
	 * <p>Description: </p>
	 * @see com.huabo.spsp.export.FileExport#clear()
	 */
	@Override
	public void clear() {
		titles = null;
		int length = builder.length();
		if (length > 0) {
			builder.delete(0, length);
		}
		list = null;
	}

	/* (non-Javadoc)
	 * <p>Title: setList</p>
	 * <p>Description: </p>
	 * @param list
	 * @see com.huabo.spsp.export.FileExport#setList(java.util.List)
	 */
	@Override
	public void setList(List<T> list) {
		this.list = list;
	}

	/* (non-Javadoc)
	 * <p>Title: write</p>
	 * <p>Description: </p>
	 * @param response
	 * @param name
	 * @see com.huabo.spsp.export.FileExport#write(javax.servlet.http.HttpServletResponse, java.lang.String)
	 */
	@Override
	public void write(HttpServletResponse response, String name) {
		PrintWriter out = null;
		try {
			response.reset();
			response.setContentType("application/csv; charset=UTF-8");
		    response.setHeader("Content-Disposition", "attachment;filename=" + new String((name + ".csv").getBytes("UTF-8"), "iso8859-1"));  
		    out = response.getWriter();
		    out.write(new String(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}));
		    out.write((String) export());
		    out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
	
	/*public static void main(String[] args) {
		FileExport<Object> exp = new ExportCsvFileTemplate<Object>(new ArrayList<Object>());
		exp.buildFile(new BuildFileCallBack<Object>() {
			@Override
			public void execute(FileExport<Object> template, Object t) {
				template.appendData(t.toString());
			}
		});
		
		//String content = (String) exp.export();
		//ExportUtils.writeCsv(response, content, "test");
	}*/
}
